package com.weasing.res.web.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.weasing.res.domain.WxPrivilege;
import com.weasing.res.domain.WxRole;
import com.weasing.res.service.IWxPrivilegeService;
import com.weasing.res.service.IWxRoleService;
import com.weasing.res.utils.RESUtils;
import com.weasing.res.web.action.base.BaseAction;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 权限action
 * 
 * @author 黄龙
 *
 */
@Controller
@Scope("prototype")
public class WxPrivilegeAction extends BaseAction<WxPrivilege> {
	@Autowired
	private IWxPrivilegeService wxPrivilegeService;
	@Autowired
	private IWxRoleService wxRoleService;
	// 权限的集合 属性驱动
	private List<Integer> privilegeIds;

	public List<Integer> getPrivilegeIds() {
		return privilegeIds;
	}

	public void setPrivilegeIds(List<Integer> privilegeIds) {
		this.privilegeIds = privilegeIds;
	}

	// 当前的角色id 的属性驱动
	public Integer roleId;

	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}

	/**
	 * 查询所有权限的信息---添加角色页面
	 * 
	 * @author 黄龙
	 */
	public String findAll() {
		// 根据id查询当前人的角色 TODO
		WxRole role = (WxRole) RESUtils.getSession().getAttribute("role");

		// 查询权限数据
		List<WxPrivilege> list = wxPrivilegeService.findAllByParentId(null);

		// getValueStack().set("privilegeList", list);

		// 获取当前角色对应的权限集合
		// Set<WxPrivilege> privileges = role.getWxPrivileges();
		// if (privileges != null && privileges.size() > 0) {
		// privilegeIds = new ArrayList<Integer>();
		// for (WxPrivilege p : privileges) {
		// privilegeIds.add(p.getId());
		// }
		// }
		RESUtils.getSession().setAttribute("wxPrivilegelist", list);
		// RESUtils.getSession().setAttribute("privilegeIds", privilegeIds);
		return "wxPrivilegelist-add";
	}

	/**
	 * 查询所有权限的信息---修改角色页面
	 * 
	 * @author 黄龙
	 */
	public String findAll_edit() {
		// 查出所有的角色
		List<WxRole> roleList = wxRoleService.findAll();
		String roleid = RESUtils.getRequest().getParameter("roleid");
		
		WxRole currentRole = new WxRole();
			// 根据id查询当前人的角色 TODO
		currentRole = (WxRole) RESUtils.getSession()
				.getAttribute("role");
		// 获取当前角色对应的权限集合的id
		List<WxPrivilege> currentPrivileges = wxPrivilegeService
						.findPrivilegeByRoleId(currentRole.getId());

		List<Integer> currentPrivilegesIds = new ArrayList<Integer>();
		for (WxPrivilege WxPrivilege : currentPrivileges) {
			currentPrivilegesIds.add(WxPrivilege.getId());
		}

		// 把当前对象转化成json格式
		JSONArray currentPrivilegesJson = JSONArray
				.fromObject(currentPrivilegesIds);
		// 查询所有的权限数据
		List<WxPrivilege> wxPrivilegelist = wxPrivilegeService
				.findAllByParentId(null);


		RESUtils.getRequest().setAttribute("currentPrivilegesIds",
				currentPrivilegesJson.toString());
		RESUtils.getRequest().setAttribute("currentRole", currentRole);
		RESUtils.getRequest().setAttribute("wxPrivilegelist", wxPrivilegelist);
		RESUtils.getRequest().setAttribute("roleList", roleList);
		// RESUtils.getSession().setAttribute("privilegeIds", privilegeIds);
		return "wxPrivilegelist-edit";
	}

	/**
	 * 根据rolename 返回所有权限的集合 异步请求
	 * 
	 * @author 黄龙
	 */
	public void findPrivilegeListByRolename() {
		String parameter = RESUtils.getRequest().getParameter("rolename");
		WxRole checkUserName = wxRoleService.checkUserName(parameter);
		// TODO 角色备注
		String remark = checkUserName.getRemark();

		List<WxPrivilege> currentPrivileges = wxPrivilegeService
				.findPrivilegeByRoleId(checkUserName.getId());

		List<Integer> currentPrivilegesIds = new ArrayList<Integer>();
		if (null != currentPrivilegesIds) {
		for (WxPrivilege WxPrivilege : currentPrivileges) {
			currentPrivilegesIds.add(WxPrivilege.getId());
		}
		}
		// 把当前对象转化成json格式
		JSONArray currentPrivilegesJson = JSONArray
				.fromObject(currentPrivilegesIds);
		// 封装 角色信息和权限信息
		JSONObject obj = new JSONObject();
		obj.put("privileges", currentPrivilegesJson);
		obj.put("remark", remark);
		System.out.println(obj);
		RESUtils.printJSON(obj.toString());

	}

	/**
	 * 删除权限信息
	 */
	public String delete() {
		WxPrivilege findById = wxPrivilegeService.findById(model.getId());
		wxPrivilegeService.delete(findById);
		return this.findAll();
	}

	/**
	 * 编辑权限信息
	 */
	public void edit() {
		// TODO 先过滤信息
		wxPrivilegeService.update(model);
	}

	/**
	 * 增加权限信息
	 */

	public void add() {
		// TODO 先过滤信息
		wxPrivilegeService.save(model);
	}
}
